'''
Created on Jan 27, 2013

@author: dheigl
'''
from IndexedVectorList import IndexedVectorList
from RandomizedList import RandomizedList
from StockList import StockList
from fin2012.SimulationList import SimulationList
from os import path
import csv
import math
import os
class Simulation:
    '''
    classdocs
    '''
    
    
    def __init__(self):
        '''
        Constructor
        '''
        
    def kil(self):
        del self
        
    def chunks1(self, l, n):
        return [l[i:i + n] for i in range(0, len(l), n)]
        
    def appendtoCSV(self,data):
        fd = open('fin2012/static/data.csv','a')
        wr = csv.writer(fd, quoting=csv.QUOTE_NONE)
        for row in data:
            wr.writerow(row)
        fd.close()
        
        
    def simulate(self, randomizedList, n):
        actualValue = []
        erg = 0.0
        for v in StockList.stockList:
            actualValue.append(v.actualValue)
            # print v.actualValue
        a = 0.0
        b = 0.0
        helper = []
        helper2 = []
        counter = 0 
        for j in xrange(n):
            print "n + ", j + 1
            sum = 0.0
            del helper[:]
            for i in xrange(len(StockList.stockList)):
                a = actualValue[i]
                b = math.exp(randomizedList.rList[j].value[i])
                # print "\n actualValue[i]= ", actualValue[i] , " * math.exp(randomizedList.rList[i].value[i]= ", randomizedList.rList[i].value[i], "erg= ", actualValue[i] * math.exp(randomizedList.rList[i].value[i])
                erg = a * b
                print erg, " = \t\t", a , " * \t e**", randomizedList.rList[j].value[i]
                helper.append(erg)
                sum += erg
                counter = 0
            print "Summe: ", sum
            '''#######variabel gestalten#######helper[0]'''
            for value in helper: 
                helper2.append(value)
            counter += 1
            del actualValue[:]
            actualValue.extend(helper)
            print "--------------"
        SimulationList.summe.append(sum)
        keys = []
        for stock in StockList.stockList:
            keys.append(stock.key)
        keys.reverse()
        for key in keys:
            helper2.insert(0, key)
        chunkedList = self.chunks1(helper2, len(StockList.stockList))
        cachedList = zip(*chunkedList)

        ergList = []
        pwd = os.getcwd()+"/fin2012/static/data.csv"
        if path.exists(pwd) and path.isfile(pwd):
            '''restliche Zeilen'''
            data = []
            for erg in cachedList:
                data.append(erg)
            self.appendtoCSV(data)
        else:
            data = open('fin2012/static/data.csv','wb')
            wr = csv.writer(data, quoting=csv.QUOTE_NONE)
            stockKeys = []
    
            '''erste Zeile'''
            anz = []
            for i in xrange(n+1):
                anz.append(i)
            wr.writerow(anz)
            '''restliche Zeilen'''
            for erg in cachedList:
                wr.writerow(erg)
